package com.xdf.docmanagementsystem.common.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @program: doc-management-system
 * @description: 文档转换类
 * @author: Zhang
 * @create: 2020-06-25 22:56
 **/
public class ConventUtil {
    static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
    static final int wdFormatPDF = 17;// word转PDF 格式

    /**
     * Excel转换PDF
     *
     * @param os
     * @param source
     * @param target
     */
    public static void wordToPDF(OutputStream os, String source, String target) throws IOException {
        ActiveXComponent app = null;
        try {
            ComThread.InitSTA(true);
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            app.setProperty("AutomationSecurity", new Variant(3));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
            File targetFile = new File(target);
            if (!targetFile.exists()) {
                targetFile.createNewFile();
            }
            Dispatch.call(doc, "ExportAsFixedFormat", target, wdFormatPDF);// word保存为pdf格式宏，值为17
            Dispatch.call(doc, "Close", false);
            IOUtil.writeFile(targetFile, os);
            targetFile.delete();
            System.gc();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (app != null)
                app.invoke("Quit", wdDoNotSaveChanges);
            if (os != null)
                os.close();
        }
    }

    /**
     * Excel转换PDF
     *
     * @param os
     * @param source
     * @param target
     */
    public static void excelToPDF(OutputStream os, String source, String target) throws IOException {
        ActiveXComponent app = null;
        Dispatch dispatch;
        try {
            ComThread.InitSTA(true);
            app = new ActiveXComponent("Excel.Application");
            app.setProperty("Visible", false);
            // app.setProperty("AutomationSecurity", false);
            dispatch = app.getProperty("Workbooks").getDispatch();
            Dispatch excel = Dispatch.invoke(dispatch, "Open", Dispatch.Method, new Object[]{
                    source,
                    new Variant(false),
                    new Variant(false)
            }, new int[9]).toDispatch();
            Dispatch.invoke(excel,
                    "ExportAsFixedFormat",
                    Dispatch.Method,
                    new Object[]{
                            new Variant(0),
                            target,
                            new Variant(0)
                    }, new int[1]);
            Dispatch.call(excel, "Close", new Variant(false));
            ComThread.Release();
            File targetFile = new File(target);
            IOUtil.writeFile(targetFile, os);
            targetFile.delete();
            System.gc();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("转换失败!");
        } finally {
            if (app != null) {
                app.invoke("Quit", new Variant[]{});
                app = null;
            }
            if (os != null)
                os.close();
        }
    }
}
